﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WAD.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace WAD.DAL
{
    /// <summary>
    /// 广告数据库操作类
    /// </summary>
    public class DaoAdvert : DAO
    {
        /// <summary>
        /// 添加一条广告
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static bool Add(MAdvert m)
        {
            String sql = "insert into advert(title,img,url,[user_id],[type])values(@title,@img,@url,@user_id,@type)";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@title", DbType.String, m.title);
            db.AddInParameter(cmd, "@img", DbType.String, m.img);
            db.AddInParameter(cmd, "@url", DbType.String, m.url);
            db.AddInParameter(cmd, "@user_id", DbType.Int32, m.user_id);
            db.AddInParameter(cmd, "@type", DbType.Int32, m.type);
            return db.ExecuteNonQuery(cmd) > 0;
        }

        /// <summary>
        /// 获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static MAdvert GetEntity(int id)
        {
            String sql = "select id,title,img,url,[user_id],type,[date] from advert where id = " + id;
            object obj = BindEntity(sql, new MAdvert());
            return obj == null ? null : (MAdvert)obj;
        }


        /// <summary>
        /// 获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Dictionary<string, object> GetDetail(int id)
        {
            String sql = "select id,title,img,url,[user_id],type,CONVERT(varchar(20), [date], 20) [date] from advert where [state] = 1 and id = " + id;
            return BindMap(sql);
        }

        /// <summary>
        /// 删除一条广告
        /// </summary>
        /// <param name="id">广告id</param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public static bool Delete(int id, int userId)
        {
            String sql = "delete advert where id = " + id + " and user_id = " + userId;
            return ExecuteNonQuery(sql) > 0;
        }

        /// <summary>
        /// 隐藏该条广告
        /// </summary>
        /// <param name="id">广告id</param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public static bool Hide(int id, int userId)
        {
            String sql = "update advert set [state] = 0 where id = " + id + " and user_id = " + userId;
            return ExecuteNonQuery(sql) > 0;
        }

        /// <summary>
        /// 隐藏多条广告
        /// </summary>
        /// <param name="id">广告id</param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public static bool Hides(String ids, int userId)
        {
            String sql = "update advert set [state] = 0 where id in (" + ids + ") and user_id = " + userId;
            return ExecuteNonQuery(sql) > 0;
        }

        /// <summary>
        /// 更新广告
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static bool Update(MAdvert m)
        {
            String sql = "update advert set title = @title,img = @img,url = @url,[type] = @type where [user_id] = @user_id and id = @id";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@title", DbType.String, m.title);
            db.AddInParameter(cmd, "@img", DbType.String, m.img);
            db.AddInParameter(cmd, "@url", DbType.String, m.url);
            db.AddInParameter(cmd, "@type", DbType.String, m.type);
            db.AddInParameter(cmd, "@user_id", DbType.Int32, m.user_id);
            db.AddInParameter(cmd, "@id", DbType.Int32, m.id);
            return db.ExecuteNonQuery(cmd) > 0;
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页显示的总条数</param>
        /// <returns></returns>
        public static PageData GetList(int pageIndex, int pageSize, int userId)
        {
            String fieldList = "id,title,img,url,[type],click,view_num,[user_id]";
            String orderBy = "[user_id] desc,id desc";
            String where = "[state] = 1 and ([user_id] = " + userId + " OR [user_id] = (select parent_id from [user] where id = " + userId + "))";
            String tableName = "v_advert";
            return GetPageList(fieldList, orderBy, pageIndex, pageSize, where, tableName);
        }

        public static string GetPhone(int advert_id)
        {
            string sql = string.Format("select top 1 phone from [user],advert  where [user].id=advert.user_id and advert.id={0}", advert_id);
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            object o = db.ExecuteScalar(cmd);
            if (o == null) return "";
            return Convert.ToString(o);
        }

        public static DataTable GetList(string ids, string feilds = "*")
        {
            if (string.IsNullOrEmpty(ids))
                return null;
            //if (ids.IndexOf(",") == -1)
            //    return DAO.GetList(string.Format("select {0} from advert where id ={1}", feilds, ids));
            //else
            //    return DAO.GetList(string.Format("select {0} from advert where id in({1})", feilds, ids));


            string sql = string.Format("select {0} from advert where id in({1})", feilds, ids);
            if (ids.IndexOf(",") == -1)
                string.Format("select {0} from advert where id ={1}", feilds, ids);

            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            return db.ExecuteDataSet(cmd).Tables[0];
        }
    }
}
